//
//  FiveStarView.m
//  Town
//
//  Created by smufs on 16/9/14.
//  Copyright © 2016年 WLP. All rights reserved.
//

#import "FiveStarView.h"

#ifndef FiveStar_Highlight 
#define FiveStar_Highlight @"ic_big_star"
#endif
#ifndef FiveStar_Default
#define FiveStar_Default @"ic_big_star_d"
#endif

@interface FiveStarView () {
    
    UIImageView *halfStar;
    NSMutableArray *_grayArray;
    NSMutableArray *_highlightArray;
}

@end

@implementation FiveStarView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = kWhiteColor;
        _starValue = 5;
        _grayArray = [NSMutableArray new];
        _highlightArray = [NSMutableArray new];
        [self initializeItems];
    }
    return self;
}

- (void) initializeItems {
    
    CGFloat starW = 12;
    CGFloat starH = 12;
    CGFloat starX = 0;
    CGFloat starY = 0;
    CGFloat margin = 5;
    UIImageView *lastImage = nil;
    for (int i = 0; i < 5; i++) {
        
        starX = lastImage ? lastImage.frame.origin.x + margin + starW : margin;
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(starX, starY, starW, starH)];
        imageView.image = [UIImage imageNamed:@"ic_big_star"];
        [self addSubview:imageView];
        imageView.hidden = YES;
        [_highlightArray addObject:imageView];
        
        UIImageView *formImage = [[UIImageView alloc] initWithFrame:CGRectMake(starX, starY, starW, starH)];
        formImage.image = [UIImage imageNamed:@"ic_big_star_d"];
        formImage.tag = i+1;
        formImage.hidden = NO;
        [_grayArray addObject:formImage];
        [self addSubview:formImage];
        
        lastImage = imageView;
    }
    
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, lastImage.frame.origin.x + starW + margin, starW);
    
    halfStar = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.frame) * _starValue / 5.0f, 0, 12, 12) ];
    halfStar.hidden = YES;
    halfStar.image = [UIImage imageNamed:@"ic_big_star_half"];
    
    [self addSubview:halfStar];
}

- (void)setStarValue:(CGFloat)starValue {
    
    _starValue = starValue;
    
    halfStar.hidden = YES;
    
//    NSAssert(_starValue <= 5.0, @"");
    // 半颗星
    if (floor(_starValue) < _starValue && _starValue < ceil(_starValue))
    {
        
        if (_grayArray.count >= (int)ceil(_starValue) && ((int)ceil(_starValue) - 1) >= 0)
        {
         
            UIImageView *image = _grayArray[(int)ceil(_starValue) - 1];
            halfStar.frame = CGRectMake(image.frame.origin.x, 0, 12, 12);
            halfStar.hidden = NO;
        }
    }
    
    for (UIImageView *image in _grayArray)
    {
        if (image.tag <= ceil(_starValue))
        {
            
            image.hidden = YES;
        }
        else
        {
        
            image.hidden = NO;
        }
    }
    
    for (int i = 0; i < 5; i++)
    {
        UIImageView *image = _highlightArray[i];
        if (i < ceil(_starValue))
        {
            image.hidden = NO;
        }
        else
        {
            image.hidden = YES;
        }
    }
}

-(CGFloat)starWidth {
    
    return 5 * 12 + 5 * 5;
}
@end
